package com.xiaoyi.pdocrlibrary;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class PaddleUtils {
    private static final String TAG = "PaddleUtils";

    public static boolean isSupportedNPU() {
        return false;
    }

    public static void copyFileFromAssets(Context context, String str, String str2) {
        try {
            BufferedInputStream bufferedInputStream;
            FileNotFoundException e;
            IOException e2;
            if (!str.isEmpty() && !str2.isEmpty()) {
                BufferedOutputStream bufferedOutputStream = null;
                bufferedInputStream = new BufferedInputStream(context.getAssets().open(str));
                BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(new FileOutputStream(new File(str2)));
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = bufferedInputStream.read(bArr);
                    if (read != -1) {
                        bufferedOutputStream2.write(bArr, 0, read);
                    } else {
                        try {
                            bufferedOutputStream2.close();
                            bufferedInputStream.close();
                            return;
                        } catch (IOException e3) {
                            e3.printStackTrace();
                            return;
                        }
                    }
                }
            }
            } catch(Exception e){
                e.printStackTrace();
            }
    }

    public static void copyDirectoryFromAssets(Context context, String str, String str2) {
        if (!str.isEmpty() && !str2.isEmpty()) {
            try {
                if (!new File(str2).exists()) {
                    new File(str2).mkdirs();
                }
                String[] list = context.getAssets().list(str);
                for (String str3 : list) {
                    String str4 = str + File.separator + str3;
                    String str5 = str2 + File.separator + str3;
                    if (new File(str4).isDirectory()) {
                        copyDirectoryFromAssets(context, str4, str5);
                    } else {
                        copyFileFromAssets(context, str4, str5);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static float[] parseFloatsFromString(String str, String str2) {
        String[] split = str.trim().toLowerCase().split(str2);
        float[] fArr = new float[split.length];
        for (int i = 0; i < split.length; i++) {
            fArr[i] = Float.parseFloat(split[i].trim());
        }
        return fArr;
    }

    public static long[] parseLongsFromString(String str, String str2) {
        String[] split = str.trim().toLowerCase().split(str2);
        long[] jArr = new long[split.length];
        for (int i = 0; i < split.length; i++) {
            jArr[i] = Long.parseLong(split[i].trim());
        }
        return jArr;
    }

    public static String getSDCardDirectory() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static Bitmap resizeWithStep(Bitmap bitmap, int i, int i2) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int max = Math.max(width, height);
        if (max > i) {
            float f = (((float) i) * 1.0f) / ((float) max);
            width = (int) Math.floor((double) (((float) width) * f));
            height = (int) Math.floor((double) (f * ((float) height)));
        }
        int i3 = width - (width % i2);
        if (i3 == 0) {
            i3 = i2;
        }
        int i4 = height - (height % i2);
        if (i4 != 0) {
            i2 = i4;
        }
        return Bitmap.createScaledBitmap(bitmap, i3, i2, true);
    }

    public static Bitmap rotateBitmap(Bitmap bitmap, int i) {
        Matrix matrix = new Matrix();
        switch (i) {
            case 2:
                matrix.setScale(-1.0f, 1.0f);
                break;
            case 3:
                matrix.setRotate(180.0f);
                break;
            case 4:
                matrix.setRotate(180.0f);
                matrix.postScale(-1.0f, 1.0f);
                break;
            case 5:
                matrix.setRotate(90.0f);
                matrix.postScale(-1.0f, 1.0f);
                break;
            case 6:
                matrix.setRotate(90.0f);
                break;
            case 7:
                matrix.setRotate(-90.0f);
                matrix.postScale(-1.0f, 1.0f);
                break;
            case 8:
                matrix.setRotate(-90.0f);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return createBitmap;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
}
